﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObjects.BO_GetObjects;
using DigiLib;

public partial class controls_admin_ModuleEdit : Admin_Controls_Base
{
    private int location_id;
    protected void Page_Load(object sender, EventArgs e)
    {
        location_id = base.getRequest("location_id", 0);
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            try
            {
                base.bindDDL(ddlType, BO_portal_module_types.SelectAll(1), "TYPE_DESCRIPTION", "MODULE_TYPE_ID", "-----", "0");
                base.bindDDL(ddlCategories, BO_portal_categories.Inside(0), "FULL_CATEGORY_NAME", "CATEGORY_ID", "-----", "0");
                base.bindDDL(ddlLocation, BO_portal_locations.SelectAll(1), "LOCATION_NAME", "LOCATION_ID", "-----", "0");
                base.bindLSB(lbxCategories, BO_portal_categories.SelectAll(), "FULL_CATEGORY_NAME", "CATEGORY_ID");
                if (Category_ID > 0)
                    lbxCategories.SelectedValue = Category_ID.ToString();
                loadModule();
            }
            catch (Exception ex) { divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message); }
        }
    }
    protected void btnModuleAction_Click(object sender, EventArgs e)
    {
        try
        {
            if (lbxCategories.Items.Count <= 0) { divMessage.InnerHtml = MessageBox.Warning("Cảnh Báo", "Yêu cầu chọn danh mục hiển thị (hiển thị tại)"); return; }
            ObjDT = BO_portal_modules.Search(Id);
            int type_id = Convert.ToInt32(ddlType.SelectedValue);
            string title = txtTitle.Text.Trim();
            int moduleType = Convert.ToInt32(ddlType.SelectedValue);
            int location = Convert.ToInt32(ddlLocation.SelectedValue);
            int sort_display = Convert.ToInt32(txtSDHome.Text.Trim());
            int category_id = Convert.ToInt32(ddlCategories.SelectedValue);
            string readMore = "";// txtLink.Text.Trim();
            int page_size = Convert.ToInt32(txtPageSize.Text);
            bool isHome = cboDisDetails.Checked;// cboDisplay.Checked;
            bool isDetails = cboDisDetails.Checked;
            string filename = string.Empty;
            DataTable admin = (DataTable)Session["Admin"];
            string strresult = BO_portal_modules.Update(Id, type_id, category_id.ToString(), title, filename, readMore, page_size, isDetails, isHome, sort_display, Convert.ToDateTime(ObjDT.Rows[0]["CREATED_DATE"]), ObjDT.Rows[0]["CREATED_BY"].ToString(), DateTime.Now, Administrator.USER_NAME, 1);
            if (strresult != "0")
            {
                bool flag = false;
                string first = string.Empty;
                foreach (ListItem item in lbxCategories.Items)
                {
                    if (item.Selected)
                    {
                        first = item.Value;
                        //if (Category_ID == Convert.ToInt32(item.Value))
                        //{
                        BO_portal_modules_sort.Update(Id, Convert.ToInt32(item.Value), location, sort_display, true); flag = true;
                        //}
                        //else BO_portal_modules_sort.Insert(Id, Convert.ToInt32(item.Value), location, sort_display, true);
                    }
                    else BO_portal_modules_sort.Delete(Id, Convert.ToInt32(item.Value));
                }
                divMessage.InnerHtml = MessageBox.Success("Thông Báo", "Module đã được cập nhật!");
                if (flag)
                    first = Category_ID.ToString();
                Response.Redirect("default.aspx?control=modules&location_id=" + ddlLocation.SelectedValue + "&category_id=" + first + "&keyword=" + Request["keyword"].ToString());
            }
            else
                divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi trong quá trình xử lý!");
        }
        catch (Exception ex) { divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message); }
    }
    private void loadModule()
    {
        try
        {
            ObjDT = BO_portal_modules.Search(Id);
            if (ObjDT != null && ObjDT.Rows.Count > 0)
            {
                txtTitle.Text = ObjDT.Rows[0]["FULL_NAME"].ToString();
                ddlType.SelectedValue = ObjDT.Rows[0]["MODULE_TYPE_ID"].ToString();
                ddlCategories.SelectedValue = ObjDT.Rows[0]["CATEGORY_ID"].ToString();
                ddlLocation.SelectedValue = location_id.ToString();
                cboDisDetails.Checked = Convert.ToBoolean(ObjDT.Rows[0]["IS_DISPLAY_ON_DETAIL"]);
                txtPageSize.Text = ObjDT.Rows[0]["PAGE_SIZE"].ToString();
                ObjDT = BO_portal_modules_sort.SelectAdmin(Id);
                foreach (DataRow dr in ObjDT.Rows)
                {
                    foreach (ListItem item in lbxCategories.Items)
                    {
                        if (dr["CATEGORY_ID"].ToString().Trim().Equals(item.Value.Trim()))
                            item.Selected = true;
                        if (Convert.ToInt32(dr["CATEGORY_ID"]) == Category_ID)
                            txtSDHome.Text = dr["ORDER_DISPLAY"].ToString();
                    }
                }
            }
        }
        catch (Exception ex) { divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message); }
    }
}
